package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.truck;


import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck.TruckBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck.TruckCallZxBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck.TruckQueueBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck.TruckQueueZxBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CCompanys;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CProductsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CTrucks;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.TruckExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICCompanysService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICTrucksService;
import com.xiaoxu.intranetweb.util.DecodeLocalHttpRequest;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/truck")
public class TruckController {
    @Autowired
    private TruckBu truckBu;
    @Autowired
    private TruckQueueBu truckQueueBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;
    @Autowired
    private TruckQueueZxBu truckQueueZxBu;
    @Autowired
    private ICProductsInfoService cProductsInfoService;

    @Autowired
    private ICCompanysService  cCompanysService;
    @Autowired
    private ICTrucksService cTrucksService;
    @GetMapping("/getTruck")
    public SzResult getTruck(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "pageSize", required = false) String pageSize,
            @RequestParam(value = "truckNo", required = false) String truckNo,
            @RequestParam(value = "truckType", required = false) String truckType,
            @RequestParam(value = "bindCoId", required = false) String bindCoId,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.getTruck(page, pageSize, truckNo, truckType,bindCoId, token, plantId);
    }
//拿到车里排队，车辆信息接口
    @GetMapping("/getTruckList")
    public SzResult getTruckList(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "pageSize", required = false) String pageSize,
            @RequestParam(value = "truckNo", required = false) String truckNo,
            @RequestParam(value = "bindCoId", required = false) String bindCoId,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.getTruckList(page, pageSize, truckNo, bindCoId, token, plantId);
    }

    @GetMapping("/getTruckQueueInfo")
    public SzResult getTruckQueueInfo(@RequestParam(value = "coId", required = false) String coId,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.getTruckQueueInfo(coId);
    }

    //获取车辆排队信息
    @GetMapping("/getAllWaitTrucks")
    public SzResult getAllWaitTrucks(
            @RequestParam(value = "dataFrom", required = false) String dataFrom,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "pdtId", required = false) String pdtId,
            @RequestParam(value = "truckNo", required = false) String truckNo,
            @RequestParam(value = "coId", required = false) String coId,
            @RequestParam(value = "icCardNo", required = false) String icCardNo,
            @RequestParam(value = "iccardAlias", required = false) String iccardAlias,
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "pageSize", required = false) String pageSize,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
//        pdtId是以逗号分隔
        List<String> pdtIdList = new ArrayList<>();

        if (pdtId != null) {
            pdtIdList = Arrays.stream(pdtId.split(","))
                    .map(String::trim)
                    .filter(s -> !s.isEmpty())
                    .collect(Collectors.toList());
        }

        return truckQueueBu.getAllWaitTrucks(token, startTime, endTime, pdtIdList, truckNo, coId, icCardNo,iccardAlias, page, pageSize, plantId);
    }

    @GetMapping("/getAllBindGpsTrucks")
    public SzResult getAllBindGpsTrucks(
            @RequestParam(value = "dataFrom", required = false) String dataFrom,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "pdtId", required = false) String pdtId,
            @RequestParam(value = "truckNo", required = false) String truckNo,
            @RequestParam(value = "coId", required = false) String coId,
            @RequestParam(value = "icCardNo", required = false) String icCardNo,
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "pageSize", required = false) String pageSize,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckQueueBu.getAllBindGpsTrucks(token, startTime, endTime, pdtId, truckNo, coId, icCardNo, page, pageSize, plantId);
    }

    //新增车辆信息
    @PostMapping("/addTruck")
    @PermissionCheck("truck_addTruck")
    public SzResult addTruck(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String trucksTruckno = jsonObject.getString("trucksTruckno");
        String trucksTrailerno = jsonObject.getString("trucksTrailerno");
        String trucksType =  jsonObject.getString("trucksType");
        Integer trucksLength = jsonObject.getInteger("trucksLength");
        Integer trucksEmiss = jsonObject.getInteger("trucksEmiss");
        Integer trucksAxles = jsonObject.getInteger("trucksAxles");
        Integer trucksRefTare = jsonObject.getInteger("trucksRefTare");//参考皮重（吨）
        Integer trucksRefNet = jsonObject.getInteger("trucksRefNet");//参考净重（吨）
        String trucksMaster = jsonObject.getString("trucksMaster");
        String trucksIdentity = jsonObject.getString("trucksIdentity");
        String trucksTell = jsonObject.getString("trucksTell");
        String bindCoId = jsonObject.getString("bindCoId");
        String backgroundCo = jsonObject.getString("backgroundCo");
        Integer ifBackgrounded = jsonObject.getInteger("ifBackgrounded");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.addTruck(trucksTruckno, trucksTrailerno, trucksType, trucksLength, trucksEmiss, trucksAxles, trucksRefTare, trucksRefNet, trucksMaster, trucksIdentity, trucksTell,bindCoId,backgroundCo,ifBackgrounded, token, plantId);
    }

    //删除车辆信息
    @GetMapping("/deleteTruck")
    @PermissionCheck("truck_deleteTruck")
    public SzResult deleteTruck(@RequestParam String trucksTruckno, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.deleteTruck(trucksTruckno, token, plantId);
    }

    //修改车辆信息
    @PostMapping("/updateTruck")
    @PermissionCheck("truck_updateTruck")
    public SzResult updateTruck(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");
        String trucksTruckno = jsonObject.getString("trucksTruckno");
        String trucksTrailerno = jsonObject.getString("trucksTrailerno");
        String trucksType = jsonObject.getString("trucksType");
        Integer trucksLength = jsonObject.getInteger("trucksLength");
        Integer trucksEmiss = jsonObject.getInteger("trucksEmiss");
        Integer trucksAxles = jsonObject.getInteger("trucksAxles");
        Integer trucksRefTare = jsonObject.getInteger("trucksRefTare");
        Integer trucksRefNet = jsonObject.getInteger("trucksRefNet");
        String trucksMaster = jsonObject.getString("trucksMaster");
        String trucksIdentity = jsonObject.getString("trucksIdentity");
        String trucksTell = jsonObject.getString("trucksTell");
        String trucksAgainMinTm = jsonObject.getString("trucksAgainMinTm");
        String bindCoId = jsonObject.getString("bindCoId");
        String backgroundCo = jsonObject.getString("backgroundCo");
        Integer ifBackgrounded = jsonObject.getInteger("ifBackgrounded");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.updateTruck(id, trucksTruckno, trucksTrailerno, trucksType, trucksLength, trucksEmiss, trucksAxles, trucksRefTare, trucksRefNet, trucksMaster, trucksIdentity, trucksTell, trucksAgainMinTm,bindCoId,backgroundCo,ifBackgrounded, token, plantId);
    }
    @PostMapping("/updateVipNum")
    public SzResult updateVipNum(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");
        Integer vipNum = jsonObject.getInteger("vipNum");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.updateVipNum(id,vipNum);
    }
//    @PostMapping("/updateBgTruck")
//    public SzResult updateBgTruck(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
//        String id = jsonObject.getString("id");
//        String backgroundCo = jsonObject.getString("backgroundCo");
//        Integer ifBackgrounded = jsonObject.getInteger("ifBackgrounded");
//        String token = request.getHeader("Authorization");
//        String plantId = request.getHeader("Plant-Id");
//        return truckBu.updateBgTruck(id,backgroundCo,ifBackgrounded);
//    }

    /**
     * 新增车辆排队信息
     */
    @PostMapping("/addWaitTrucks")
    public SzResult addWaitTrucks(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String CardInfCardNo = jsonObject.getString("CardInfCardNo");
        String truckNo = jsonObject.getString("truckNo");
        String queuePdtId = jsonObject.getString("queuePdtId");
        String co = jsonObject.getString("co");
        String chiCo = jsonObject.getString("chiCo");
        String plantId = request.getHeader("Plant-Id");
      /*  String queueLastStatus = jsonObject.getString("queueLastStatus");
        String queueStatus = jsonObject.getString("queueStatus");*/
        String token = request.getHeader("Authorization");
        return truckQueueBu.addWaitTrucks(CardInfCardNo, truckNo, queuePdtId, co, chiCo, token, plantId);
    }

    /**
     * IC卡类型判断
     *
     * @param CardInfCardNo
     * @return
     */
    @GetMapping("/IcCardJudge")
    public SzResult IcCardJudge(@RequestParam String CardInfCardNo, HttpServletRequest request) {
        String plantId = request.getHeader("Plant-Id");
        return truckQueueBu.IcCardJudge(CardInfCardNo, plantId);
    }


    /**
     * 修改车辆排队信息
     */
    @GetMapping("/updateWaitTrucks")
    @PermissionCheck("truck_updateWaitTrucks")
    public SzResult updateWaitTrucks(@RequestParam String id, @RequestParam(required = false) String targetStatus, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckQueueBu.upDateWaitTrucks(token, id, targetStatus, plantId);
    }

    /**
     * 导出车辆信息
     */
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
//        String plantId = null;
//        if (token != null) {
//            plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//        }
        //        实体类
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
//        文件名使用utf-8编码
        String filename = jsonParam.getString("filename");
//        数据
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
//        是否有表头
        Boolean isHeader = jsonParam.getBoolean("isHeader");
//        mode为"current"时，导出当前页，为"all"时，导出所有页，selected时，导出选中数据
        String mode = jsonParam.getString("mode");
//        是否原始数据
        Boolean original = jsonParam.getBoolean("original");
//        分页
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
//        表格标题
        String sheetName = jsonParam.getString("sheetName");
        Map form = jsonParam.getObject("form", Map.class);
//        String truckNo, Integer truckType
        String truckNo = null;
        if (form.get("truckNo") != null && !form.get("truckNo").equals("null")) {
            truckNo = form.get("truckNo").toString();
        }
        Integer truckType = null;
        if (form.get("truckType") != null && !form.get("truckType").equals("null")) {
            truckType = Integer.parseInt(form.get("truckType").toString());
        }
        Future<List<TruckExcelEn>> listFuture = truckBu.exportExcel(fields, filename, ids, isHeader, mode, original, pager, sheetName, plantId, truckNo, truckType);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<TruckExcelEn> truckExcelEns = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (filename == null || filename.equals("")) {
            filename = "车辆信息";
        }
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), TruckExcelEn.class)
                .sheet("车辆信息")
                .registerWriteHandler(horizontalCellStyleStrategy)
                .doWrite(truckExcelEns);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }


    @PostMapping("/getIcCardList")
    public SzResult getIcCardList(HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.getIcCardList(token, plantId);
    }


    //查询车辆排队记录
    @PostMapping("/getWaitTrucksRecord")
    public SzResult getWaitTrucksRecord(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String truckNo = jsonParam.getString("truckNo");
        String cardNo = jsonParam.getString("cardNo");
        String cpyId = jsonParam.getString("cpyId");
        String page = jsonParam.getString("page");
        String pageSize = jsonParam.getString("pageSize");
        String startTime = jsonParam.getString("startTime");
        String endTime = jsonParam.getString("endTime");
        return truckQueueBu.getWaitTrucksRecord(truckNo, cardNo, cpyId, page, pageSize, plantId,startTime,endTime);
    }

    //删除车辆排队记录
    @GetMapping("/deleteWaitTrucksRecord")
    public SzResult deleteWaitTrucksRecord(@RequestParam String id, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckQueueBu.deleteWaitTrucksRecord(id, plantId);
    }

//    直接使用车号排队

    @PostMapping("/startQueueByTruckNo")
    public SzResult startQueueByTruckNo(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
        // jsonParam转成map
        Map<String, Object> map = jsonParam.getInnerMap();

        String token = null;
        if (request.getHeader("Authorization") != null) {
            token = request.getHeader("Authorization");
        }

        String plantId = null;

        // 从请求头中获取 Plant-Id
        plantId = request.getHeader("Plant-Id");

        // 如果请求头中没有 Plant-Id，则尝试从参数 map 中获取
        if (plantId == null) {
            if (map != null && map.containsKey("plantId")) {
                Object plantIdObj = map.get("plantId");
                if (plantIdObj != null) {
                    plantId = plantIdObj.toString();
                } else {
                    return SzResult.error("厂的id不能为空");
                }
            } else {
                return SzResult.error("厂的id不能为空");
            }
        }

        // 如果是本地发过来的请求会有Local-Transmission，检查请求头是否需要解码
//        String localTransmission = request.getHeader("Local-Transmission");
//        boolean needDecode = false;
//        if (localTransmission != null) {
//            needDecode = "GBK".equalsIgnoreCase(localTransmission);
//        }
        // 获取参数
//        String cardinfTruckTypeValue = map.get("cardinfTruckTypeValue") != null ?
//                (needDecode ? DecodeLocalHttpRequest.decodeGb2312IfNecessary(map.get("cardinfTruckTypeValue").toString()) : map.get("cardinfTruckTypeValue").toString()) : "";
//        String cardinfTruckPwd = map.get("cardinfTruckPwd") != null ?
//                (needDecode ? DecodeLocalHttpRequest.decodeGb2312IfNecessary(map.get("cardinfTruckPwd").toString()) : map.get("cardinfTruckPwd").toString()) : "";
//        String cardinfCompany = map.get("cardinfCompany") != null ?
//                (needDecode ? DecodeLocalHttpRequest.decodeGb2312IfNecessary(map.get("cardinfCompany").toString()) : map.get("cardinfCompany").toString()) : "";
        //        String cardinfTruckTypeValue, String cardinfTruckPwd ,
        String cardinfTruckTypeValue = map.get("cardinfTruckTypeValue").toString();
        String cardinfTruckPwd = map.get("cardinfTruckPwd").toString();
//        cardinfCompany
        String cardinfCompany = map.get("cardinfCompany").toString();
        return SzResult.success(truckQueueZxBu.startQueueByTruckNoZx(token, cardinfCompany, cardinfTruckTypeValue, cardinfTruckPwd, plantId));
    }





    //    直接使用车辆id排队
    @PostMapping("/startQueueByTruckNoLocal")

    public SzResult startQueueByTruckNoLocal(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
        // jsonParam转成map
        Map<String, Object> map = jsonParam.getInnerMap();
        String token = null;
        if (request.getHeader("Authorization") !=null) {
            token = request.getHeader("Authorization");
        }
        String plantId = null;

        // 从请求头中获取 Plant-Id
        plantId = request.getHeader("Plant-Id");

        // 如果请求头中没有 Plant-Id，则尝试从参数 map 中获取
        if (plantId == null) {
            if (map != null && map.containsKey("plantId")) {
                Object plantIdObj = map.get("plantId");
                if (plantIdObj != null) {
                    plantId = plantIdObj.toString();
                } else {
                    return SzResult.error("厂的id不能为空");
                }
            } else {
                return SzResult.error("厂的id不能为空");
            }
        }

//        String cardinfTruckTypeValue, String cardinfTruckPwd ,
        String cardinfTruckTypeValue = map.get("cardinfTruckTypeValue").toString();
        String cardinfTruckPwd = map.get("cardinfTruckPwd").toString();
//        cardinfCompany
        String cardinfCompany = map.get("cardinfCompany").toString();
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", cardinfTruckTypeValue);
        CProductsInfo cProductsInfo = cProductsInfoService.getOne(queryWrapper);
        if (cProductsInfo == null) {
            return SzResult.error("没有找到该品种");
        }
        QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID", cardinfCompany);
        CCompanys cCompanys = cCompanysService.getOne(queryWrapper1);
        if (cCompanys == null) {
            return SzResult.error("没有找到该公司");
        }
QueryWrapper<CTrucks> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("ID", cardinfTruckPwd);
        CTrucks cTrucks = cTrucksService.getOne(queryWrapper2);
        if (cTrucks == null) {
            return SzResult.error("没有找到该车号");
        }
        return   SzResult.success(truckQueueZxBu.startQueueByTruckNoZx(token,cCompanys.getCpyCo()+"."+cCompanys.getCpyChico(), cProductsInfo.getPdtinfPdt(),cTrucks.getTrucksTruckno(), plantId));
    }
    @PostMapping("/weighTest")
    public SzResult weighTest(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
// jsonParam转成map
        String truckNo = jsonParam.getString("truckNo");
        return truckQueueBu.weighTest(truckNo);
    }
    @PostMapping("/returnWeighTest")
    public SzResult returnWeighTest(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
// jsonParam转成map
        String truckNo = jsonParam.getString("truckNo");
        return truckQueueBu.returnWeighTest(truckNo);
    }

//     //    查询可手动排队车辆列表
//    public SzResult getQueueTruckList(String page, String pageSize, String truckNo, String bindCoId, String token, String plantId) {
    @GetMapping("/getQueueTruckList")
    public SzResult getQueueTruckList(@RequestParam String page, @RequestParam String pageSize, @RequestParam(required = false) String truckNo, @RequestParam(required = false) String bindCoId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBu.getQueueTruckList(page, pageSize, truckNo, bindCoId, token, plantId);
    }

    //获取等待的车辆排队信息
    @GetMapping("/getAllWaitingTrucks")
    public SzResult getAllWaitingTrucks(
            @RequestParam(value = "truckNo", required = false) String truckNo,
            @RequestParam(value = "coId", required = false) String coId,
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "pageSize", required = false) String pageSize,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckQueueBu.getAllWaitingTrucks(token,  truckNo, coId,  page, pageSize, plantId);
    }

    //获取叫号的车辆排队信息
    @GetMapping("/getAllCallingTrucks")
    public SzResult getAllCallingTrucks(
            @RequestParam(value = "truckNo", required = false) String truckNo,
            @RequestParam(value = "coId", required = false) String coId,
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "pageSize", required = false) String pageSize,
            HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckQueueBu.getAllCallingTrucks(token,  truckNo, coId,  page, pageSize, plantId);
    }
//    取消排队
      @GetMapping("/cancelQueue")
    public SzResult cancelQueue(@RequestParam String id, HttpServletRequest request) throws Exception {
          String token = request.getHeader("Authorization");
          String plantId = request.getHeader("Plant-Id");
          return truckQueueBu.cancelQueue(id, plantId);
      }
//本地排队机取消排队接口
    @PostMapping("/cancelTruckQueue")
    public SzResult cancelTruckQueue(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
        String id = jsonParam.getString("id");
        String plantId = jsonParam.getString("plantId");
        return truckQueueBu.cancelTruckQueue(id, plantId);
    }
//      手动叫号
//==================执行叫号逻辑============================
//public SzResult  manualCallin(String id, String areaName, String token, String plantId) {
    @PostMapping("/manualCallin")
    public SzResult manualCallin(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String id = jsonParam.getString("id");
        String ashGateId = jsonParam.getString("ashGateId");
        return truckQueueBu.manualCallin(id, ashGateId, token, plantId);
}

//        //       手动叫号这个单位可以选到的装载点
//    public SzResult handCallInTruckByAshGate(String trucksCheckinPdt, String trucksCheckinCo) throws Exception {
    @GetMapping("/handCallInTruckByAshGate")
    public SzResult handCallInTruckByAshGate(@RequestParam String trucksCheckinPdt, @RequestParam String trucksCheckinCo, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckQueueBu.handCallInTruckByAshGate(trucksCheckinPdt, trucksCheckinCo);
    }
//    获取不同仓库的车数
    @GetMapping("/getAshGateTruckCount")
    public SzResult getTruckCount(HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckQueueBu.getAshGateTruckCount(token, plantId);
    }
}

